package com.tensquare.base.controller;


import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("label")
@CrossOrigin        //允许跨域访问
@RefreshScope       //表示配置文件中自定义的配置可以随springcloud-bus的刷新而改变(自定义配置需要，其他不用)
public class LabelController {

    @Autowired
    private LabelService labelService;
    @Value("${ip}")
    private String ip;

    //1.查询Label全部列表
    @GetMapping
    public Result findAll(){
        try {
            System.out.println("ip = "+ip);
            //1.1查询标签列表
            List<Label> labelList=labelService.findAll();
            //1.2返回结果
            return new Result(true,StatusCode.OK,"查询数据成功!",labelList);
        } catch (Exception e) {
            e.printStackTrace();
            //1.2返回结果
            return new Result(false,StatusCode.ERROR,"查询数据失败!");
        }
    }
    //2.添加标签
    @PostMapping
    public Result add(@RequestBody Label label){
        try {
            //2.1调用服务插入标签
            labelService.insert(label);
            //2.2返回结果
            return new Result(true,StatusCode.OK,"添加标签成功！");
        } catch (Exception e) {
            e.printStackTrace();
            //2.2返回结果
            return new Result(false,StatusCode.ERROR,"添加标签失败！");
        }
    }
    //3.根据id查询标签
    @GetMapping("{labelId}")
    public Result getLabelById(@PathVariable String labelId){
        try {
            //3.1调用服务查询标签
            Label label=labelService.getOne(labelId);
            //3.2返回结果
            return new Result(true,StatusCode.OK,"查询标签成功！",label);
        } catch (Exception e) {
            e.printStackTrace();
            //3.2返回结果
            return new Result(false,StatusCode.ERROR,"查询标签失败！");
        }
    }
    //4.根据id修改标签
    @PutMapping("{labelId}")
    public Result updateById(@PathVariable String labelId,@RequestBody Label label){
        try {
            //4.1给标签id赋值
            label.setId(labelId);
            //4.2调用服务修改标签
            labelService.update(label);
            //4.3返回结果
            return new Result(true,StatusCode.OK,"修改标签成功！");
        } catch (Exception e) {
            e.printStackTrace();
            //4.3返回结果
            return new Result(false,StatusCode.ERROR,"修改标签失败！");
        }
    }
    //5.根据id删除标签
    @DeleteMapping("{labelId}")
    public Result delete(@PathVariable String labelId){
        try {
            //5.1调用服务进行删除
            labelService.delete(labelId);
            //5.2返回结果
            return new Result(true,StatusCode.OK,"删除标签成功！");
        } catch (Exception e) {
            e.printStackTrace();
            //5.2返回结果
            return new Result(false,StatusCode.ERROR,"删除标签失败！");
        }
    }
    //6.根据条件进行查询
    @PostMapping("search")
    public Result findSearch(@RequestBody Label label){
        //6.1调用服务进行查询
        List<Label> labelList=labelService.findSearch(label);
        //6.2返回查询结果
        return new Result(true,StatusCode.OK,"条件查询成功！",labelList);
    }
    //7.带条件分页查询
    @PostMapping("search/{page}/{size}")
    public Result search(@PathVariable int page,@PathVariable int size,@RequestBody Label label){
        //7.1跟据条件进行查询
        PageResult<Label> labels=labelService.findSearch(label,page,size);
        //7.2返回结果
        return new Result(true,StatusCode.OK,"查询成功！",labels);
    }

}
